<?php
/**
 *@regexCheck: 正则匹配函数
 *@param  string  $pattern  [指定匹配规则]
 *@param string   $str      [要验证的字符串]
 *@author: wzk
 *@return bool    $is_match [匹配成功返回 true ，失败 false]
 *@date: 2019-07-14 18:46:48
*/
function regexCheck($pattern,$str){
    static $regex = array(
        'email'     => "/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/",
        'phone'     => '/^([1]\d{10}|([\(（]?0[0-9]{2,3}[）\)]?[-]?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)$/',
        // 'phone'     => "/^((1(([356789][0-9])|(47)))\d{8})$/",
    );
    
    $match = null;
    if (preg_match($regex["$pattern"],trim($str),$match)) {
        return true;
    } else {
        return false;
    }
    
    return $match;
}

/**
 * 导入数据时根据手机号查重
 *
 * @param array  $arr     [要查重的数据]
 * @param string $key     [指定要对数组中的哪个键数据进行查重 比如手机号 zxr_tel]
 * @param array  $tmp_arr [如果arr中数据的手机号在此数组中则为重复]
 * @return void
 */
function assoc_unique($arr, $key, $tmp_arr)
{
    list($data, $repeatArr) = $arr;
    if( !empty($tmp_arr) ){
        foreach ($data as $k => $v) {
            if (in_array($v[$key], $tmp_arr)) { //搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                $data[$k]['msg'] = '重复数据';
                $repeatArr[] = $data[$k];
                unset($data[$k]);
            } else {
                $tmp_arr[] = $v[$key];
            }
        }
    }
    
    sort($data); //sort函数对数组进行排序

    return [$data, $repeatArr];
}
//隐藏手机号部分数字
function phonenumhide ($phone) {
    return substr_replace($phone, '****', 3, 4);
}
//获取状态对应id
function getStatusId($str) {
    $status_config = array(
        '沉默客户'=>0,
        '已签约'=>1,
        '签约率'=>2,
        '新资源'=>3,
        '旧资源'=>4,
        '长期客户'=>5,
    );
    $contract_rate_select=[
        ['id'=>0,'name'=>'签约率90%以上'],
        ['id'=>1,'name'=>'签约率80%'],
        ['id'=>2,'name'=>'签约率50%'],
        ['id'=>3,'name'=>'签约率20%'],
    ];

    $str = trim($str);
    foreach( $status_config as $k=>$v ) {
        if( strstr($str, $k) ){
            if($v == 2) {
                foreach($contract_rate_select as $vo) {
                    if( strstr($str, $vo['name']) )
                        return array(
                            'sstatus'=>$v,
                            'contract_rate'=>$vo['id']
                        );
                }
            }
            return $v;
        }
    }
    return -1;
}
//获取咨询顾问对应id
function getConsultantId($str,$consultant_list) {
    $str = trim($str);
    foreach( $consultant_list as $k=>$v ) {
        if( strstr($str, $k) ) return $v;
    }
    return -1;
}

//获取接待员对应id
function getReceptionistId($str, $receptionist_list) {
    $str = trim($str);
    foreach( $receptionist_list as $k=>$v ) {
        if( strstr($str, $k) ) return $v;
    }
    return -1;
}

//去除当前用户无权查看的数据
function delDataOnConsultant($data, $key, $consultants) {
    foreach($data as $k=>$vo) {
        if(!in_array($vo[$key], $consultants)){
            unset($data[$k]);
        }
    }
    return $data;
}

function proStartTime() {
    global $startTime;
    $mtime1 = explode(" ", microtime());
    $startTime = $mtime1[1] + $mtime1[0];
}
function proEndTime($str='执行时间') {
    global $startTime,$set;
    $mtime2 = explode(" ", microtime());
    $endtime = $mtime2[1] + $mtime2[0];
    $totaltime = ($endtime - $startTime);
    $totaltime = number_format($totaltime, 7);
    echo "<br/>$str: ".$totaltime;
    proStartTime();
}
